package com.loyal.loyalerp.humenresource.personimage.api;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.loyal.loyalerp.humenresource.employee.entity.Gy_Person;
import com.loyal.loyalerp.humenresource.personimage.data.PersonImageVo;
import com.loyal.loyalerp.humenresource.personimage.entity.Rs_PersonImage;
import com.loyal.loyalerp.humenresource.personimage.exception.ImageNoE;
import com.loyal.loyalerp.humenresource.personimage.service.PersonImageReadService;
import com.loyal.loyalerp.humenresource.personimage.service.PersonImageWriteService;
import com.loyal.loyalerp.utils.ValidateUtil;
@Controller
@RequestMapping("/personImage")
public class PersonImageApiSource {
	
	@Autowired
	private PersonImageReadService personImageReadService;
	@Autowired
	private PersonImageWriteService personImageWriteService;
	
	@RequestMapping("/jsp")
	public String toPage() {
		return "HumenResource/PersonImage/personImageList";
	}
	@RequestMapping("/editjsp")
	public String toEditPage() {
		return "HumenResource/PersonImage/personImageEdit";
	}
	@ResponseBody
	@RequestMapping("/list")
	public List<PersonImageVo> personImageList() {
		return personImageReadService.getList();
	}
	
	@ResponseBody 
	@RequestMapping("/add")
	public Map<String, Object> addPerson(HttpServletRequest re,@RequestParam("personName")String personName,@RequestParam("imageName")String imageName,@RequestParam("file")MultipartFile uploadfiles) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(uploadfiles==null) {
			map.put("status", 400);
			return map;
		}

		String imageUrl = addImage(re,uploadfiles);
		map.put("url", imageUrl);

		if (ValidateUtil.validateUser() == null) {
			map.put("status", 400);
			map.put("msg", "用户登录已经失效");
			return map;
		}
		
		personImageWriteService.add(personName,imageName,imageUrl);
		map.put("status", 200);
		return map;
	}
	
	public String addImage(HttpServletRequest re,@RequestParam("file")MultipartFile uploadfiles) {
	       String ret_path = re.getSession().getServletContext().getRealPath("")+File.separator+"resources";
	       String uuid = uploadCopyImage(uploadfiles,ret_path);
	       String url = ret_path+File.separator+uuid+uploadfiles.getOriginalFilename();
	       return url;
	}
	
	
	public  String uploadCopyImage(MultipartFile uploadfiles,String path) {
		 File file = new File(path);
	        if (!file.exists()) {
	            file.mkdirs();
	        }
	        FileInputStream fileInputStream = null;
			try {
				fileInputStream = (FileInputStream) uploadfiles.getInputStream();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
	        BufferedOutputStream bos = null;
	        String uuid = UUID.randomUUID().toString(); //获取UUID并转化为String对象  
	        uuid = uuid.replace("-", "");
			try {
				bos = new BufferedOutputStream(new FileOutputStream(path+File.separator+uuid+uploadfiles.getOriginalFilename()));
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			}
	        byte[] bs = new byte[1024];
	        int len;
        	try {
				while ((len = fileInputStream.read(bs)) != -1) {
					bos.write(bs, 0, len);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
	        try {
				bos.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
	        try {
				bos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
	        return uuid;
	}
	
	
	
}
